package com.xxx.system.controller;


import com.github.pagehelper.PageInfo;
import com.xxx.base.exception.BusinessException;
import com.xxx.base.utils.R;
import com.xxx.system.annotation.PreAuthorize;
import com.xxx.system.domain.Logininfo;
import com.xxx.system.dto.LoginDTO;
import com.xxx.system.query.LogininfoQuery;
import com.xxx.system.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类: 登录信息Logininfo
 */
@RestController
@RequestMapping("/logininfo")
@Api(tags = "登录信息接口类")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 接口: 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有接口")
    @GetMapping
    @PreAuthorize(name = "查询所有登录信息",sn = "logininfo:getAll",descs = "查询所有登录信息")
    public R<List<Logininfo>> findAll(){
        List<Logininfo> list = logininfoService.findAll();
        return R.ok(list);
    }

    /**
     * 接口: 查询单个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个登录信息",sn = "logininfo:getOne",descs = "根据Id查询登录信息")
    public R<Logininfo> findOne(@PathVariable("id")Long id){
        Logininfo logininfo = logininfoService.findOne(id);
        return R.ok(logininfo);
    }

    /**
     * 接口: 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除登录信息",sn = "logininfo:delete",descs = "删除登录信息")
    public R<Void> delete(@PathVariable("id")Long id){
        try {
            logininfoService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }

    /**
     * 接口: 添加和修改
     * @param logininfo
     * @return
     */
    @PutMapping
    @PreAuthorize(name = "保存登录信息",sn = "logininfo:save",descs = "保存登录信息")
    public R<Void> addOrUpdate(@RequestBody Logininfo logininfo){
        try {
            if(logininfo.getId() == null){//添加
                logininfoService.add(logininfo);
            }else{
                logininfoService.update(logininfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return  R.ok();
    }

    /**
     * 接口: 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public R<Void> patchDelete(@RequestBody Long[] ids){
        try {
            logininfoService.patchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return  R.ok();
    }

    /**
     * 接口: 分页查询 + 高级查询
     * @param logininfoQuery
     * @return
     */
    @PostMapping
    public PageInfo<Logininfo> queryPage(@RequestBody LogininfoQuery logininfoQuery){
        return logininfoService.queryPage(logininfoQuery);
    }

    @PostMapping("/login")
    public R<String> login(@RequestBody LoginDTO loginDTO) {
        //"jwt",alt shift z
        //try
        try {
            String jwt= logininfoService.login(loginDTO);//假设法
            return R.ok("登录成功", jwt);
        }catch (BusinessException e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return R.fail("服务器忙");
        }
    }
}
